package com.quan.weixin.api.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest;
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.quan.common.enums.DateStyle;
import com.quan.common.utils.DateUtils;
import com.quan.common.utils.RandomUitl;
import com.quan.weixin.api.service.RedpackService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class RedpackServiceImpl implements RedpackService {

	@Autowired
	private WxPayService wxPayService;

	@Override
	public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest redpackRequest, String ip) throws Exception {
		try {

			// 订单号：mch_id+yyyymmdd+10
			String mchBillNo = this.wxPayService.getConfig().getMchId() + DateUtils.format(new Date(), DateStyle.YYYYMMDD) + RandomUitl.generateInteger(10);

			redpackRequest.setMchBillNo(mchBillNo);
			redpackRequest.setClientIp(ip);
			WxPaySendRedpackResult result = this.wxPayService.getRedpackService().sendRedpack(redpackRequest);

			log.info("--------------------红包：{}--------------------", result.getReturnMsg());
			return result;
		} catch (WxPayException e) {
			throw new Exception(e);
		}
	}

}
